<?php

/**
 * 索引控制器
 *
 * @author cdlei
 */

require 'CommonController.php';

class Admin_IndexController extends Admin_CommonController
{
    /**
     * 初始化
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        
        $this->view->title = $this->_configs['project']['systemName'] . ' - 管理中心';
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        //获取当前用户的角色信息
        $currentRoleName = NULL;
        $myRoleRows = Application_Model_UserLogin::getInstance()->getUserCurrentAllRole();
        $roleId = Application_Model_UserLogin::getInstance()->getUserCurrentRoleId();
        foreach($myRoleRows as $myRoleRow) {
            if ($myRoleRow['id'] == $roleId) {
                $currentRoleName = $myRoleRow['name'];
                break;
            }
        }
        $this->view->myRoleRows = $myRoleRows;
        $this->view->currentRoleName = $currentRoleName;

        //获取当前用户产品信息
        $userLoginModel = Application_Model_UserLogin::getInstance();
        $myProductRows = $userLoginModel->getUserCurrentAllProduct();
        $currentProductCode = $userLoginModel->getUserCurrentProductCode();
        $currentProductName = NULL;
        foreach($myProductRows as $myProductRow) {
            if ($myProductRow['code'] == $currentProductCode) {
                $currentProductName = $myProductRow['name'];
                break;
            }
        }
        $this->view->myProductRows = $myProductRows;
        $this->view->currentProductCode = $currentProductCode;
        $this->view->currentProductName = $currentProductName;

        //生成菜单信息
        $productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
        $userRow = Application_Model_UserLogin::getInstance()->getLoginedRow();
        $menuModel = new Application_Model_Menu();
        $productMenuModel = new Application_Model_ProductMenu();
        $userRoleModel = new Application_Model_UserRole();
        $userRoleProductModel = new Application_Model_UserRoleProduct();
        $roleMenuModel = new Application_Model_RoleMenu();
        $templateContent = file_get_contents(APPLICATION_PATH . '/configs/userMenuTemplate.php');
        $where = "`isDisplay` = 1 AND `status` = '1' AND 
        (
            `type` = '1' AND `id` IN (
                SELECT `rm`.`menuId` FROM 
                /*角色菜单*/
                `{$roleMenuModel->getTableName()}` AS `rm`
                /*用户角色*/
                INNER JOIN `{$userRoleModel->getTableName()}` AS `ur` ON `rm`.`roleId` = `ur`.`roleId`
                /*产品菜单*/
                INNER JOIN `{$productMenuModel->getTableName()}` AS `pm` ON `pm`.`menuId` = `rm`.`menuId`
                WHERE `pm`.`productCode` = '{$productCode}' 
                    AND `ur`.`userId` = {$userRow['id']}
                    AND `ur`.`roleId` = '{$roleId}'
            ) 
        OR 
            `type` = '2' AND `id` IN (
                SELECT `rm`.`menuId` FROM `{$roleMenuModel->getTableName()}` AS `rm`
                INNER JOIN `{$userRoleModel->getTableName()}` AS `ur` ON `rm`.`roleId` = `ur`.`roleId`
                WHERE `ur`.`userId` = {$userRow['id']} AND `ur`.`roleId` = '{$roleId}'
            )
        )";
        $currentData = array('recursive'=>1, 'recursiveChildrenTag'=>'{%children%/}');
        $this->view->listContent = $menuModel->makeChildrenListString(0, $templateContent, $currentData, $where);

        //设置有效在线时间
        $this->view->effectiveOnlineTime = $this->_configs['project']['admin']['effectiveOnlineTime'];
    }

    /**
     * 切换产品
     * 
     * @return void
     */
    public function changeProductAction()
    {
        $userLoginModel = Application_Model_UserLogin::getInstance();

        $productCode = $this->_request->get('productCode');
        if (!empty($productCode)) {
            $userLoginModel->changeProduct($productCode);
            redirect($this->view->projectUrl(array('action'=>'index')));
        } else {
            echo $this->view->message('没有产品编号参数。') ;
            exit;
        }
    }

    /**
     * 切换角色
     * 
     * @return void
     */
    public function changeRoleAction()
    {
        $userLoginModel = Application_Model_UserLogin::getInstance();

        $roleId = $this->_request->get('roleId');
        if (!empty($roleId)) {
            $userLoginModel->changeRole($roleId);
            redirect($this->view->projectUrl(array('action'=>'index')));
        } else {
            echo $this->view->message('没有找到角色ID参数。') ;
            exit;
        }
    }

    /**
     * 右框架动作
     * 
     * @return void
     */
    public function rightAction()
    {
        $this->view->currentUserRow = $this->_currentUserRow;
        $infoModel = new Application_Model_Info();
        $sql = "SELECT * FROM {$infoModel->getTableName()} WHERE `class`='1' ORDER BY id DESC";
        $rows = $infoModel->getAdapter()->fetchAll($sql);
        $this->view->rows = $rows;
    }
}